package com.jk.controller;

import com.jk.bean.*;
import com.jk.service.UserService;
import com.jk.utils.ReceivePage;
import groovy.util.logging.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Slf4j
@Validated
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    @ResponseBody
    @RequestMapping("queryWeekSentiment")
    public SendPage queryWeekSentiment(ReceivePage rp){


        return userService.queryWeekSentiment(rp);
    }
    //微博登录
    @RequestMapping("tologin")
    @ResponseBody
    public String tologin(User user, HttpSession session){
          User  user1=userService.tologin(user);
          if(user1!=null){
                session.setAttribute("user",user1);
              return  "1";
          }else{

              return  "0";
          }
    }
    //去出session里面的对象
    @RequestMapping("checkUser")
    @ResponseBody
    public User checkUser(HttpSession session){
        //获取session值
       User user= (User) session.getAttribute("user");
        if(user!=null){
            return  user;
        }else{

            return null;
        }
    }
    @RequestMapping("checkUsers")
    @ResponseBody
    public Integer ok(HttpSession session){
        //获取session值
        User user= (User) session.getAttribute("user");
        if(user!=null){
            int oo=user.getUserId();
            return  oo;
        }else{

            return 0;
        }
    }
    //退出登录
    @ResponseBody
    @RequestMapping("loginout")
    public void loginout(HttpSession session){
        session.removeAttribute("user");
       // return   "1";
    }

    @ResponseBody
    @RequestMapping("addASentiment")
    public String addASentiment(String id){
        userService.addASentiment(id);
        return "1";
    }


   //添加文章点击量 addArticleAll
    @ResponseBody
    @RequestMapping("addArticleAll")
    public String addArticleAll(String id, String tableName){
         userService.addArticleAll(id,tableName);
        return "1";
    }

    @ResponseBody
    @RequestMapping("querySpace")
    public List<Content> querySpace(String id){


        return userService.querySpace(id);
    }

    @ResponseBody
    @RequestMapping("queryAuthor")
    public Author queryAuthor(String id){
        Author author = userService.queryAuthor(id);
        return author;
    }
 @ResponseBody
    @RequestMapping("queryUserbyid")
    public User queryUserbyid(String id){
     User user = userService.queryUserbyid(id);
        return user;
    }


    @ResponseBody
    @RequestMapping("queryWeekSentiment2")
    public List<Author> queryWeekSentiment2(ReceivePage rp){


        return userService.queryWeekSentiment2(rp);
    }

    @ResponseBody
    @RequestMapping("querynoAuthor")
    public List<Author> querynoAuthor(String id){
        List<Author> authorsList = userService.querynoAuthor(id);
        return authorsList;
    }

    @ResponseBody
    @RequestMapping("querybkcount")
    public Finance querybkcount(Finance fn){

        return userService.querybkcount(fn);
    }
    @ResponseBody
    @RequestMapping("remove")
    public String remove(HttpSession session, HttpServletResponse response, HttpServletRequest request){

        Cookie[] cookies = request.getCookies();

        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("login")){
                continue;
            }else {
                cookie.setValue(null);
                cookie.setMaxAge(0);// 立即销毁cookie
                cookie.setPath("/");
                System.out.println("被删除的cookie名字为:"+cookie.getName());
                response.addCookie(cookie);
            }

        }

      session.invalidate();
        return "";
    }

    @ResponseBody
    @RequestMapping("addQQ")
    public User addQQ(QQ qq){

        userService.addQQ(qq);

        User user = new User();
        user.setUserAccout(qq.getFigureurl1());
        user.setUserPwd(qq.getFigureurl2());
        return user;
    }

}
